package com.pansoft.openplanet.util;

import com.efounder.utils.CommonDecimalFormat;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * Created by YQS on 2018/3/26.
 */

public class BalanceUtil {


    /**
     * 转换能量币余额显示方式
     *
     * @param balance
     * @return
     */
    public static String balanceConvert(String balance) {
        if (balance == null) {
            return null;
        } else if ("0".equals(balance)) {
            return "0";
        } else if ("".equals(balance)) {
            return "0";
        } else {
            BigDecimal bigDecimal = new BigDecimal(balance);

            BigDecimal b2 = new BigDecimal(Double.toString(1000000000000000000D));
            double result = bigDecimal.divide(b2, 8, BigDecimal.ROUND_HALF_UP).doubleValue();
//            return String.valueOf(result);
            return new CommonDecimalFormat("0.00000000").format(result);

        }
    }

    /**
     * 转换为无小数点
     *
     * @param balance
     * @return
     */
    public static BigDecimal balanceToBiInteger(String balance) {
        if (balance == null) {
            return null;
        } else if ("".equals(balance)) {
            return null;
        } else if ("0".equals(balance)) {
            return new BigDecimal("0");
        } else {
            BigDecimal bigInteger = new BigDecimal(balance).multiply(BigDecimal.valueOf(1000000000000000000D));
            return bigInteger;
        }
    }

    /**
     * 转换为无小数点
     *
     * @param balance
     * @return
     */
    public static BigDecimal btcToBiInteger(String balance) {
        if (balance == null) {
            return null;
        } else if ("".equals(balance)) {
            return null;
        } else if ("0".equals(balance)) {
            return new BigDecimal("0");
        } else {
            BigDecimal bigInteger = new BigDecimal(balance).multiply(BigDecimal.valueOf(100000000D));
            return bigInteger;
        }
    }

    public static String formatPrice(double price, boolean halfUp, int digits) {
        if (price==0){
            return "0";
        }
        CommonDecimalFormat formater = new CommonDecimalFormat();
        // keep 2 decimal places
        //设置小数点后最大位数为8
        formater.setMaximumFractionDigits(digits);
        //设置小数点后最小位数为8
        formater.setMinimumFractionDigits(digits);
        //设置每四3个一组 默认
        formater.setGroupingSize(3);
        formater.setRoundingMode(halfUp ? RoundingMode.HALF_UP : RoundingMode.FLOOR);
        return formater.format(price);
    }


    public static void main(String[] args) {
        String pattern = "###.##";
        double number = 123423.45;

        Locale enlocale  = new Locale("en", "US");
        Locale dalocale  = new Locale("da", "DK");

        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(enlocale);
        decimalFormat.applyPattern(pattern);

        System.out.println(decimalFormat.format(number));


        decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(dalocale);
        decimalFormat.applyPattern(pattern);

        System.out.println(decimalFormat.format(number));


        double number1 = 121223232473.4567;
        Locale enlocale1  = new Locale("ru", "RU");

        DecimalFormat decimalFormat1 = new DecimalFormat();
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(enlocale1);
        symbols.setPatternSeparator(',');
        symbols.setGroupingSeparator(',');
        decimalFormat1.setDecimalFormatSymbols(symbols);
        System.out.println(number1);
        System.out.println(decimalFormat1.format(number1));


        decimalFormat.setGroupingSize(4);
        System.out.println(decimalFormat1.format(number1));

    }
}
